		
function checksession(){
	home();
	$.getJSON('../controllers/login.php?',{action:3},function(data) {		
		if(data.status == "ok"){
			$("#message").html("Bienvenido");
			//setTimeout('$("#message").hide(1000)',3000);
			html = "<ul id='nav2'><li> | Bienvenido "+data.username+"</li><li><a onclick='logout()'> | Salir </a></li>";
			if(data.type > 1)
			html+="<li><a onclick='getHtml_admin(0)'> | Admin </a></li></ul>";
			$("#hp2").html(html);
			html = '<img src="../includes/images/user_icon.png" class="icon_user"/>';
			$("#hp3").html(html);
		}
	});
}
//validamos los datos de entrada del usuario y realizamos el login correspondiente

function login(){
	color = "#F7D1D1";	
	color2 = "#EEEEEE";
	usr = trim($("#usr").val());
	pdw = trim($("#pdw").val());	
	img = "<img src='../includes/images/ajax.gif' />";
	
	$("#message").html(img);	
	
	//checar campos vacios
	if(pdw == "" || usr == "" ){
		if(pdw == "" ) $("#pdw").css("backgroundColor",color); else $("#pdw").css("backgroundColor", color2);
		if(usr == "" )$("#usr").css("backgroundColor",color);	else $("#usr").css("backgroundColor", color2);
		$("#message").html("Debes llenar todos los campos.");
		return;
	}
	$.getJSON('../controllers/login.php?',{action:1,username:usr,password:pdw},function(data) {		
		if(data.status == "error"){			
			
			$("#message").html(data.error);
			//setTimeout('$("#message").hide(1000)',3000);
		}
		else {
			checksession();
		}
		//
	});	
}
function logout(){
		
	img = "<img src='../includes/images/ajax.gif' />";
	$("#message").show(1000);
	$("#message").html(img);
	
	$.getJSON('../controllers/login.php?',{action:2},function(data) {
		
		if(data.status == "error"){			
			color = "#F7D1D1";
			$("#usr").css("backgroundColor",color);
			$("#pdw").css("backgroundColor",color);
			$("#message").html(data.error);
			//setTimeout('$("#message").hide(1000)',3000);
		}
		else {
			
			$("#message").html("Regresa pronto.");
			//setTimeout('$("#message").hide(1000)',3000);
			setTimeout('location.reload(true);',3000);
			
		}
		//
	});	
}


function  register(){
	color = "#F7D1D1";
	color2 = "#EEEEEE";
	img = "<img src='../includes/images/ajax.gif' />";
	$("#message").html(img);
	pwd = trim($("#pwd2").val());
	usr = trim($("#usr2").val());
	ml = trim($("#mail2").val());
	//checar campos vacios
	if(pwd == "" || usr == "" || ml == ""){
		if(pwd == "" ) $("#pwd2").css("backgroundColor",color); else $("#pwd2").css("backgroundColor",color2);
		if(usr == "" ) $("#usr2").css("backgroundColor",color); else $("#usr2").css("backgroundColor",color2);
		if(ml == "") $("#mail2").css("backgroundColor",color);  else $("#mail2").css("backgroundColor",color2);
		$("#message").html("Debes llenar todos los campos.");
		return;
	}
	$.getJSON('../controllers/login.php?',{action:4,username:usr,password:pwd,mail:ml},function(data) {
		
		if(data.status == "error"){					
			$("#message").html(data.error);			
			//setTimeout('$("#message").hide(1000)',3000);
		}
		else {
			home();
			$("#message").html("Registro exitoso.Ahora pudes iniciar sesion.");
			//setTimeout('$("#message").hide(1000)',3000);
			//setTimeout('location.reload(true);',3000);
			
		}
		//
	});	
}